#
# Kbuild file for klibc
#

# Tell that we are building klibc
export klibc-build := y

klib-y := vsnprintf.o snprintf.o vsprintf.o sprintf.o \
	  asprintf.o vasprintf.o \
	  vsscanf.o sscanf.o ctypes.o \
	  strntoumax.o strntoimax.o \
	  atoi.o atol.o atoll.o \
	  strtol.o strtoll.o strtoul.o strtoull.o \
	  strtoimax.o strtoumax.o \
	  globals.o exit.o atexit.o onexit.o \
	  execl.o execle.o execv.o execvpe.o execvp.o execlp.o execlpe.o \
	  fork.o vfork.o wait.o wait3.o waitpid.o system.o \
	  setpgrp.o getpgrp.o daemon.o \
	  printf.o vprintf.o fprintf.o vfprintf.o perror.o \
	  statfs.o fstatfs.o umount.o \
	  creat.o open.o openat.o open_cloexec.o \
	  fopen.o fread.o fread2.o fgetc.o fgets.o \
	  fwrite.o fwrite2.o fputc.o fputs.o puts.o putchar.o \
	  sleep.o usleep.o strtotimespec.o strtotimeval.o \
	  raise.o abort.o assert.o alarm.o pause.o \
	  __signal.o sysv_signal.o bsd_signal.o siglist.o sigabbrev.o \
	  siglongjmp.o \
	  sigaction.o sigpending.o sigprocmask.o sigsuspend.o \
	  pselect.o ppoll.o \
	  brk.o sbrk.o malloc.o realloc.o calloc.o \
	  mmap.o shm_open.o shm_unlink.o \
	  memcpy.o memcmp.o memset.o memccpy.o memmem.o memswap.o \
	  memmove.o memchr.o memrchr.o \
	  strcasecmp.o strncasecmp.o strndup.o strerror.o strsignal.o \
	  strcat.o strchr.o strcmp.o strcpy.o strdup.o strlen.o strnlen.o \
	  strncat.o strlcpy.o strlcat.o \
	  strstr.o strncmp.o strncpy.o strrchr.o \
	  strxspn.o strspn.o strcspn.o strpbrk.o strsep.o strtok.o \
	  fnmatch.o \
	  gethostname.o getdomainname.o getcwd.o \
	  seteuid.o setegid.o \
	  getenv.o setenv.o putenv.o __put_env.o unsetenv.o \
	  clearenv.o nullenv.o \
	  getopt.o getopt_long.o readdir.o remove.o \
	  syslog.o closelog.o pty.o getpt.o isatty.o reboot.o \
	  time.o utime.o llseek.o nice.o getpriority.o \
	  qsort.o bsearch.o \
	  lrand48.o jrand48.o mrand48.o nrand48.o srand48.o seed48.o \
	  inet/inet_ntoa.o inet/inet_aton.o inet/inet_addr.o \
	  inet/inet_ntop.o inet/inet_pton.o inet/bindresvport.o \
	  send.o recv.o \
	  ctype/isalnum.o ctype/isalpha.o ctype/isascii.o \
	  ctype/isblank.o ctype/iscntrl.o ctype/isdigit.o \
	  ctype/isgraph.o ctype/islower.o ctype/isprint.o \
	  ctype/ispunct.o ctype/isspace.o ctype/isupper.o \
	  ctype/isxdigit.o ctype/tolower.o ctype/toupper.o \
	  userdb/getgrgid.o userdb/getgrnam.o userdb/getpwnam.o \
	  userdb/getpwuid.o userdb/root_group.o userdb/root_user.o

klib-$(CONFIG_KLIBC_ERRLIST) += errlist.o

ifeq ($(CONFIG_KLIBC_ERRLIST),y)
KLIBCCFLAGS_strerror.o += -DWITH_ERRLIST
endif

klib-$(CONFIG_KLIBC_ZLIB) += zlib/
# arch specific .o files
klib-y += arch/$(KLIBCARCHDIR)/

#####
# Shared definitions
LIBC     := libc.a
SOLIB    := libc.so
SOHASH   := klibc.so
CRT0     := arch/$(KLIBCARCHDIR)/crt0.o
INTERP_O := interp.o

always   := $(LIBC) $(SOLIB) $(SOHASH) $(INTERP_O)
LIBC     := $(call objectify,$(LIBC))
SOLIB    := $(call objectify,$(SOLIB))
SOHASH   := $(call objectify,$(SOHASH))
CRT0     := $(call objectify,$(CRT0))
INTERP_O := $(call objectify,$(INTERP_O))

SOLIBHASH = $(shell cat $(SOLIB).hash)

ifndef __TCS__
# Generate syscall stubs
klib-y += syscalls/
# Generate socket calls stubs
klib-y += socketcalls/
endif

#####
# Readable errormessages extracted from src..
targets += errlist.c
quiet_cmd_errlist = GEN     $@
      cmd_errlist = $(PERL) $< $(KLIBCCPPFLAGS) -errlist > $@ || rm -f $@

$(obj)/errlist.c: $(srctree)/$(src)/makeerrlist.pl
	$(call cmd,errlist)


# all .o files for all dirs
klib-o-files = $(shell cat $(obj)/klib.list \
		$(addsuffix /klib.list, $(klib-dirs)))
######
# Build static library: libc.a
targets += libc.a __static_init.o
ifndef __TCS__
quiet_cmd_libc = KLIBCAR $@
      cmd_libc = rm -f $@;						\
                 $(KLIBCAR) cq $@					\
		 $(call objectify,__static_init.o) $(klib-o-files);	\
                 $(KLIBCRANLIB) $@
else
quiet_cmd_libc = KLIBCAR $@
      cmd_libc = rm -f $@;						\
                 $(KLIBCAR) cr $@					\
		 $(call objectify,__static_init.o) $(klib-o-files)
endif

$(LIBC): $(call objectify,__static_init.o) $(obj)/klib.list FORCE
	$(call if_changed,libc)

#
#TriMedia has no mmu, so no share library.
#
ifndef __TCS__
######
# Build shared library
targets += libc.so __shared_init.o

quiet_cmd_libcso = KLIBCLD $@
      cmd_libcso = $(KLIBCLD) $(KLIBCLDFLAGS) $(KLIBCSHAREDFLAGS) -o $@ \
			--start-group					\
				$(CRT0)					\
				$(call objectify,__shared_init.o)	\
				$(klib-o-files)				\
				$(KLIBCLIBGCC)				\
			--end-group

$(SOLIB): $(call objectify,__shared_init.o) $(obj)/klib.list FORCE
	$(call if_changed,libcso)

#####
# Build sha1 hash values
targets     += klibc.so libc.so.hash
hostprogs-y := sha1hash
clean-files += klibc-???????????????????????????.so

quiet_cmd_solibhash = HASH    $@
      cmd_solibhash = $(KLIBCNM) $< | egrep '^[0-9a-fA-F]+ [ADRTW] ' | \
                                        sort | $(obj)/sha1hash > $@
$(SOLIB).hash: $(SOLIB) $(obj)/sha1hash FORCE
	$(call if_changed,solibhash)

quiet_cmd_sohash = GEN     $@
      cmd_sohash = cat $< > $@;                                           \
                     $(KLIBCSTRIP) $(KLIBCSTRIPFLAGS) $@;                 \
		     chmod a+x $@;					  \
                     rm -f $(obj)/klibc-???????????????????????????.so;   \
                     ln -f $@ $(obj)/klibc-$(SOLIBHASH).so
$(SOHASH): $(SOLIB) $(SOLIB).hash
	$(call cmd,sohash)
endif


#####
# build interp.o
targets += interp.o

quiet_cmd_interp = BUILD   $@
      cmd_interp = $(KLIBCCC) $(klibccflags) -D__ASSEMBLY__     \
                             -DLIBDIR=\"$(SHLIBDIR)\"         \
			     -DSOHASH=\"$(SOLIBHASH)\" \
			     -c -o $@ $<

$(INTERP_O): $(obj)/interp.S $(SOLIB).hash
	$(call if_changed,interp)

#####
# Install klibc
install-rule:
	@echo "  INSTALL klibc to $(INSTALLROOT)$(INSTALLDIR)/$(KLIBCCROSS)lib"
	$(Q)$(foreach f, $(LIBC) $(SOLIB) $(CRT0) $(INTERP_O), \
	  $(shell $(install-data) $(f) \
	          $(INSTALLROOT)$(INSTALLDIR)/$(KLIBCCROSS)lib))
	$(Q)$(install-lib) $(obj)/klibc-$(SOLIBHASH).so \
	                      $(INSTALLROOT)$(INSTALLDIR)/$(KLIBCCROSS)lib
	$(Q)$(install-lib) $(obj)/klibc-$(SOLIBHASH).so \
	                      $(INSTALLROOT)$(SHLIBDIR)
